package cn.st4rlight.util.exception;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import cn.st4rlight.util.check.DataCheckResult;
import cn.st4rlight.util.check.SimpleCheckResult;
import cn.st4rlight.util.response.ResultCode;
import cn.st4rlight.util.value.NumberUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 用于做一些基础的参数校验，抛出可忽略异常ValidateException，研发不感知
 *
 * @author st4rlight <st4rlight@163.com>
 * Created on 2025-05-03
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BizAssert {

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new ValidateException(message);
        }
    }

    public static void isFalse(boolean expression, String message) {
        if (expression) {
            throw new ValidateException(message);
        }
    }

    public static void isTrue(boolean expression, ResultCode resultCode) {
        if (!expression) {
            throw BizException.of(resultCode);
        }
    }

    public static void isNull(Object object, String message) {
        isTrue(Objects.isNull(object), message);
    }

    public static void notNull(Object object, String message) {
        isTrue(Objects.nonNull(object), message);
    }

    public static void isPositive(Integer number, String message) {
        isTrue(NumberUtil.isPositive(number), message);
    }

    public static void isPositive(Long number, String message) {
        isTrue(NumberUtil.isPositive(number), message);
    }

    public static void isPositive(String number, String message) {
        isTrue(NumberUtil.isPositive(number), message);
    }

    public static <T> void notEmpty(Collection<T> collection, String message) {
        isTrue(CollectionUtils.isNotEmpty(collection), message);
    }

    public static void notBlank(String str, String message) {
        isTrue(StringUtils.isNotBlank(str), message);
    }

    public static <T> void isPresent(Optional<T> optional, String message) {
        isTrue(optional.isPresent(), message);
    }

    public static <T> void equals(T actual, T expected, String message) {
        isTrue(Objects.equals(actual, expected), message);
    }

    public static void isTrue(SimpleCheckResult checkResult) {
        isTrue(checkResult.hasPassCheck(), checkResult.getRejectReason());
    }

    public static <T> void isTrue(DataCheckResult<T> checkResult) {
        isTrue(checkResult.getCheckResult());
    }
}
